package com.springark.ms.sampleexternals.service;

import cn.hankchan.utils.HttpClientApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;

/**
 * @author hankchan
 * @since 2019/5/9
 */
@Service
public class ExternalsService {

    @Resource
    private HttpClientApi httpClient;

    public Object get() {
        // 网关入口
        String gatewayHost = "localhost:8080";

        // 微服务名称
        String serviceName = "sample-goods";

        // 拼接目标微服务的 uri
        String url = "http://" + gatewayHost + "/" + serviceName + "/goods/get";

        // 传入网关请求 access_token 参数
        Map<String, String> params = Collections.singletonMap("access_token", "123456789");

        // 通过网关发送接口请求
        try {
            return httpClient.getByUrlEncoded(url, params);
        } catch (Exception e) {
            return null;
        }
    }
}
